// 申请支付
import Vue from 'vue'
import {store} from '@/store/index'
import imgs from '@/img_path.json'
import carPriceListMods  from '@/components/ivew_conf/car_price_list_mods'
import carPriceList  from '@/components/ivew_components/car_price_list/index.vue'
import { deepCopy } from '../../../utils/tools';
import pay from '@/components/form_tools/funs/car_pay'
import commonFn from './common_fn.ts'


const vueObj:any = new Vue()

// 创世纪pos机支付码
const csjPosCode = '123456789-12345';

// 创世纪转账ID
const csjPaymentID = '1234567890123456'; 

// 创世纪支付二维码
const csjCode = '/8/f/e/8fe3d63da495944edf16ac0ba8c4ad1e.jpg';


const conf = {

    title: '申请支付',
    icon: 'icon-loan',
    pag: 'submistPlay',
    fn (){

            let orderData = store.state['carFormStore']['orderData'];

            let priceData = {
                jqPrice: orderData.SDjqAmount,
                syPrice: orderData.SDsyAmount,
                tax: orderData.SDtaxAmount,
                payment: ''
            }
    
            let mods = carPriceListMods(true);
            
            // 配置只读
            mods.forEach(mod =>{
               
                mod.conf.disabled =  mod.key === 'payment' ? false : true;
                
            })
    
            // 值判断
            if(orderData.SDjqAmount === undefined){
                throw 'jqAmount is undefind'
            }
            
            vueObj.$Modal.confirm(
                {   
                    onOk (){
    
                        // 取两位小数
                        Object.keys(priceData).forEach(key =>{
                            if(key !== 'payment'){
                                priceData[key] = parseFloat(priceData[key])
                            }
                        })


                        let map  = {
                            
                            // 创世纪支付
                            csXj (){
                                selfPayment(priceData);
                            },
                            csjZz (){
                                selfPayment(priceData);
                            },
                            csjCode(){
                                selfPayment(priceData);
                            },

                            // 保险公司支付
                            bxGrZz: baoMath,
                            bxGrCode: baoMath,
                            bxGdgZz: baoMath,
                            bxGdgCode: baoMath
                        }
                        
                        map[priceData['payment']](priceData)
                        
                        
                    },
                    render (h){
                        return h(carPriceList, {
                            props:{
                                data: priceData,
                                mods,
                            }
                        })
                    }            
                }
            )
            
            
        
    }
    
}

// 创世纪支付处理, 自动通过
function selfPayment(priceData){
    submitPay(priceData['payment'])
    payPass()
}

// 保险公司支付处理, 等待后台处理
function baoMath(priceData){

    submitPay(priceData.payment).then(success =>{

        if(success){
            vueObj.$Notice.success({
                title: '提交成功'
            })

            out()
        }
        
    })
}


function submitPay(payment){

    return store.dispatch('request', {
                            
        url: 'patch/order/id/status',
        data: {

            currentAction: '申请缴费',
            message: payment,

        }
        
    })
    
}

// 自动审核通过后, 提示缴费
function payPass (){

    store.dispatch('request', {
                            
        url: 'patch/order/id/status',
        data: {
            currentAction: '待缴费',
            qrCodeUrl: csjCode,
            posCode: csjPosCode,
            payId: csjPaymentID,
        }
        
    }).then(success =>{

        if(success){

            setTimeout(()=>{

                vueObj.$Modal.confirm({
                    title: '申请已通过,是否开始支付?',
                    okText: '是',
                    cancelText: '否',
                    onOk (){

                        let orderId = store.state['carFormStore']['orderData']['Did']

                        // 显示支付窗
                        store.dispatch('request', {url: 'get/orders/id' ,data: { orderId }}).then(success =>{

                            setTimeout(() =>{

                                if(success){
                                    pay.fn()
                                }
                                
                            }, 500)
                            
                        })


                    },
                    onCancel(){
                        commonFn('updateTable','outForm')
                    }
                    
                })
                
            }, 500)

        }
        
    })
    
}

// 跳出
function out (){

    setTimeout(() =>{

        commonFn('updateTable','outForm')
        
    }, 500)
    
}



export default conf